package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.client.ProductFeignClient;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ListFeignClient listFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map<String, Object> getItem(Long skuId) {

        Map<String ,Object> map = new HashMap<>();

        try {
            //异步编排
            //创建一个线程查询skuInfo
            CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
                SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                map.put("skuInfo",skuInfo);
                return skuInfo;
            },threadPoolExecutor);


            //  获取分类数据
            CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                //  调用
                BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
                map.put("categoryView", categoryView);
            },threadPoolExecutor);


            CompletableFuture<Void> productViewCompletableFuture = CompletableFuture.runAsync(()->{
                BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
                map.put("price",skuPrice);
            },threadPoolExecutor);
            //  销售属性回显
            CompletableFuture<Void> spuCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                List<SpuSaleAttr> spuSaleAttrList = this.productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
                //  保存数据
                map.put("spuSaleAttrList", spuSaleAttrList);
            },threadPoolExecutor);

            //  获取切换的json 字符串
            CompletableFuture<Void> valueCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                Map idsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
                String strJson = JSON.toJSONString(idsMap);
                map.put("valuesSkuJson", strJson);
            },threadPoolExecutor);

            //  获取海报信息
            CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
                List<SpuPoster> spuPosterList = this.productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
                map.put("spuPosterList",spuPosterList);
            },threadPoolExecutor);

            //  规格与包装
            CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
                //  平台属性：特殊待遇.....
                List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);
                //  页面显示的时候，需要的attrName：平台属性名 ,attrValue：平台属性值的名称！
                List<HashMap<String, Object>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                    HashMap<String, Object> mp = new HashMap<>();
                    mp.put("attrName", baseAttrInfo.getAttrName());
                    //  请问为什么是 0 ? 1 2 3呢?
                    mp.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                    return mp;
                }).collect(Collectors.toList());
                map.put("skuAttrList", skuAttrList);

            },threadPoolExecutor);

            //调用热度排名
            CompletableFuture<Void> incrCompletableFuture = CompletableFuture.runAsync(() -> {
                listFeignClient.incrHotScore(skuId);
            },threadPoolExecutor);


            //多任务组合
            CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture,productViewCompletableFuture,
                    spuCompletableFuture,valueCompletableFuture,spuPosterCompletableFuture,attrCompletableFuture
                    ,incrCompletableFuture).join();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭线程池
            //threadPoolExecutor.shutdown();
        }
        //  返回数据
        return map;
    }
}
